<?php
/**
 * Created by PhpStorm.
 * User: Zhoujinda
 * Date: 2016/3/29
 * Time: 14:04
 */
namespace Ucenter\Api;

use Common\Api\Api;
use Ucenter\Model\MemberModel;

class MemberExpApi extends Api{

    const EXP_INC = "Ucenter/MemberExp/expInc";

    const EXP_DEC = "Ucenter/MemberExp/expDec";

    const GET_USER_RANK = "Ucenter/MemberExp/getUserRank";

    //初始化
    protected function _init(){
        $this->model = new MemberModel();
    }


    public function expInc($uid,$exp){

        $map = array(
            'uid' => $uid,
        );

        $result = $this -> model -> where($map) -> setInc('score',$exp);

        if($result === false){
            $error = $this -> model -> getDbError();
            return $this -> apiReturnErr($error);
        }else{
            return $this -> apiReturnSuc($result);
        }

    }

    /**
     * 减去用户积分
     * @param $uid
     * @param $score 输入为正数
     */
    public function expDec($uid,$exp){
        $map = array(
            'uid' => $uid,
        );

        $result = $this -> model -> where($map) -> setDec('exp',$exp);
        if($result === false){
            $error = $this -> model -> getDbError();
            return $this -> apiReturnErr($error);
        }else{
            return $this -> apiReturnSuc($result);
        }
    }

    /**
     * 判断用户等级
     */
    public function getUserRank($uid){

        /**
         * 1级  1-5
         * 2级  6-15
         * 3级  16-30
         * 4级  31-50
         * 5级  51-100
         * 6级  101-200
         * 7级  201-500
         * 8级  501-1000
         * 9级  1001-2000
         * 10级  2001-3000
         * 11级  3000-6000
         * 12级  6000-10000
         * 13级  10001-18000
         * 14级  18001-30000
         * 15级  30001-60000
         * 16级  60001-100000
         * 17级  100001-3000000
         * 18级  300000+
         */

        $rank = array(
            'VIP1' => array(1,5),
            'VIP2' => array(6,15),
            'VIP3' => array(16,30),
            'VIP4' =>  array(31,50),
            'VIP5' => array(51,100),
            'VIP6' => array(101,200),
            'VIP7' => array(201,500),
            'VIP8' => array(501,1000),
            'VIP9' => array(1001,2000),
            'VIP10' => array(2001,3000),
            'VIP11' => array(3000,6000),
            'VIP12' => array(6000,10000),
            'VIP13' => array(10001,18000),
            'VIP14' => array(18001,30000),
            'VIP15' => array(30001,60000),
            'VIP16' => array(60001,100000),
            'VIP17' => array(100001,300000),
        );




        $map = array('uid'=>$uid);

        $result = $this->model->where($map)->find();
        if($result === false){
            $error = $this -> model -> getDbError();
            return $this -> apiReturnErr($error);
        }else{

            $return = '';
            if(is_null($result)){
                return $this->apiReturnErr('该用户不存在');
            }else{

                $exp = $result['exp'];
                if($exp == 0){
                    $return = 'VIP0';

                }elseif($exp <= 3000000){
                    foreach($rank as $key => $val){

                        if($exp>=$val[0] && $exp<=$val[1]){
                            $return = $key;
                            break;
                        }

                    }


                }elseif($exp>300000){
                    $return = 'VIP18';
                }

                return $this->apiReturnSuc(array('uid'=>$uid,'rank'=>$return));

            }

        }

    }
}